from django import template
from django.test.client import Client
from django.conf import settings

register = template.Library()

class TrueSSINode(template.Node):
    def __init__(self,  url):
        self.url = url
        
    def render(self, context):
        if getattr(settings, 'SSI_ON', True):
            client = Client()
            if 'request' in context:
                client.cookies = context['request'].COOKIES
            return client.get(url).content
        else:
            return u'<!--#include virtual="%s" -->' % self.url

@register.tag
def truessi(parser, token):
    try:
        tag_name, arg = token.contents.split()
    except ValueError:
        raise template.TemplateSyntaxError, "%r tag requires arguments" % token.contents.split()[0]
    return TrueSSINode(arg)